import React, {useState} from 'react';
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import {Button, Checkbox, Form, Input, message} from 'antd';
import styles from "./index.module.less";
import {useNavigate} from "react-router-dom";
import store from "@/store";
import {postLogin} from "@/api/auth";
import {useSelector} from "react-redux";
import {changeLoading, useSelectorAuth} from "@/store/slices/auth";
import {authRequest} from "@/request";
const Login: React.FC = () => {
    const navigate = useNavigate();
    // 获取数据状态1
    // console.log(store.getState());// {auth:{loading:false}}

    // 获取数据状态2: useSelector接收一个回调函数，回调函数返回的值即是useSelector返回的值
    //             回调函数接收的参数即是数据状态
    // const result =  useSelector(state=>{
    //     console.log(state);// {auth:{loading:false}}
    // });
    // console.log(result);//


    // 获取loading
    // type TAuth = {
    //     auth:{
    //         loading:boolean
    //     }
    // }
    // const {loading} = useSelector((state:TAuth)=>state.auth);
    // console.log(loading);

    // 获取数据状态3：封装方法
    const {loading} = useSelectorAuth();
    console.log(loading);


    return (
        <div className={styles.login}>
            <div>
                <h1>尚医通管理系统</h1>
                <Form
                    name="normal_login"
                    className="login-form"
                    initialValues={{ remember: true }}
                    onFinish={async (values:any)=>{
                        try{
                            store.dispatch(changeLoading(true));
                            const result:any = await postLogin(values);
                            message.success(result.message);
                            localStorage.setItem("token",result.data.token);
                            store.dispatch(changeLoading(false));
                            navigate("/");
                        }catch (err){
                            store.dispatch(changeLoading(false));
                        }

                    }}
                >
                    <Form.Item
                        name="username"
                        rules={[{ required: true, message: '请输入管理员账号!' }]}
                    >
                        <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="管理员账号" />
                    </Form.Item>
                    <Form.Item
                        name="password"
                        rules={[{ required: true, message: '请输入管理员密码' }]}
                    >
                        <Input
                            prefix={<LockOutlined className="site-form-item-icon" />}
                            type="password"
                            placeholder="管理员密码"
                        />
                    </Form.Item>

                    <Form.Item>
                        <Button loading={loading}  type="primary" htmlType="submit" className="login-form-button">
                            登陆
                        </Button>
                    </Form.Item>
                </Form>
            </div>

        </div>

    );
};

export default Login;